﻿using ScreenSaverDigitalClock.eventlistener;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ScreenSaverDigitalClock
{
    public partial class FormDigitalClock : Form
    {
        private TimeScreenSaver panelTimeScreenSaver;
        public FormDigitalClock()
        {
            InitializeComponent();
            FullScreenShow();
            panelTimeScreenSaver = new TimeScreenSaver();
            this.Controls.Add(panelTimeScreenSaver);
            panelTimeScreenSaver.Size = this.ClientSize;
            panelTimeScreenSaver.Location = new Point(0, 0);
            InitMouseAndKeyboardListener();
        }

        /*** 设置窗体全屏显示 *************************************************************************************/
        protected void FullScreenShow()
        {
            this.SetVisibleCore(false);
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.SetVisibleCore(true);
        }
        /**********************************************************************************************************/

        /*** 防止刷新闪屏 *****************************************************************************************/
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            panelTimeScreenSaver.Invoke(new Action(() => { panelTimeScreenSaver.RefreshTime(); }));
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        MouseHook mouseHook = new MouseHook();
        KeyboardHook keyboardHook = new KeyboardHook();
        private void InitMouseAndKeyboardListener()
        {
            mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
            mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
            mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
            mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);

            keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
            keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
            keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress);

            mouseHook.Start();
            keyboardHook.Start();
            SetXYLabel(MouseSimulator.X, MouseSimulator.Y);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        void keyboardHook_KeyPress(object sender, KeyPressEventArgs e)
        {
            AddKeyboardEvent("KeyPress", "", e.KeyChar.ToString(), "", "", "");
        }

        void keyboardHook_KeyUp(object sender, KeyEventArgs e)
        {
            AddKeyboardEvent("KeyUp", e.KeyCode.ToString(), "", e.Shift.ToString(), e.Alt.ToString(), e.Control.ToString());
        }

        void keyboardHook_KeyDown(object sender, KeyEventArgs e)
        {
            AddKeyboardEvent("KeyDown", e.KeyCode.ToString(), "", e.Shift.ToString(), e.Alt.ToString(), e.Control.ToString());
        }

        void mouseHook_MouseWheel(object sender, MouseEventArgs e)
        {
            AddMouseEvent("MouseWheel", "", "", "", e.Delta.ToString());
        }

        void mouseHook_MouseUp(object sender, MouseEventArgs e)
        {
            AddMouseEvent("MouseUp", e.Button.ToString(), e.X.ToString(), e.Y.ToString(), "");
        }

        void mouseHook_MouseDown(object sender, MouseEventArgs e)
        {
            AddMouseEvent("MouseDown", e.Button.ToString(), e.X.ToString(), e.Y.ToString(), "");
        }

        void mouseHook_MouseMove(object sender, MouseEventArgs e)
        {
            SetXYLabel(e.X, e.Y);
        }

        void SetXYLabel(int x, int y)
        {
            // curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y);
        }

        void AddMouseEvent(string eventType, string button, string x, string y, string delta)
        {
            // listView1.Items.Insert(0, new ListViewItem(new string[]{eventType, button, x, y, delta}));
            this.Close();
        }

        void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)
        {
            // listView2.Items.Insert(0, new ListViewItem(new string[]{eventType, keyCode, keyChar, shift, alt, control}));
            this.Close();
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
}
